<template>
  <el-container class="home-container">
    <el-header>
      <div>
        <img src="@/assets/heima.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="warning" plain @click="loginOut">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!-- 侧边栏折叠按钮 -->
        <div class="toggle-button" @click="toggleeCollapse">|||</div>
        <el-menu background-color="#313743" text-color="#fff" active-text-color="#389cff" unique-opened :collapse="isCollapse" :collapse-transition="false" router :default-active="activePath">
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for="item in MenuList" :key="item.id">
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconObj[item.id]"></i>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="'/' + item.path" v-for="item in item.children" :key="item.id" @click="saveNavState('/' + item.path)">
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>
                  {{ item.authName }}
                </span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'MyHome',
  data() {
    return {
      // 菜单数据
      MenuList: [],
      // 图标数据
      iconObj: {
        125: 'el-icon-user-solid',
        103: 'el-icon-circle-plus',
        101: 'el-icon-shopping-bag-2',
        102: 'el-icon-s-order',
        145: 'el-icon-data-line'
      },
      // 控制侧边栏是否折叠
      isCollapse: false,
      // 被激活的链接地址
      activePath: ''
    }
  },
  created() {
    this.getMenuList()
    // 取出需要激活的二级菜单对应的值并赋值
    this.activePath = sessionStorage.getItem('activePath')
  },
  methods: {
    // 退出登录的逻辑
    loginOut() {
      sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取侧边栏的数据
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.MenuList = res.data
    },
    // 点击折叠事件
    toggleeCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 在sessionStorage中存储激活的二级菜单的index值，以便刷新之后重新激活
    saveNavState(activePath) {
      sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 0;
  background-color: #363d40;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}

.el-aside {
  background-color: #313743;
  transition: all 0.5s;
  .toggle-button {
    background-color: #475163;
    text-align: center;
    color: #fff;
    line-height: 24px;
    font-size: 12px;
    letter-spacing: 0.2em;
    cursor: pointer;
  }
  .el-menu {
    // box-sizing: content-box;
    border-right: none;
  }
}

.el-main {
  background-color: #e9edf1;
}
</style>
